<template>
  <div class="directorys">
    <el-card>
      <!-- 面包屑 -->
      <el-breadcrumb
        separator-class="el-icon-arrow-right"
        v-show="showRentParise"
      >
        <el-breadcrumb-item>学科管理</el-breadcrumb-item>
        <el-breadcrumb-item>{{ this.$route.query.name }}</el-breadcrumb-item>
        <el-breadcrumb-item>目录管理</el-breadcrumb-item>
      </el-breadcrumb>
      <!-- 开头 -->
      <el-form :inline="true" :model="formInline" class="demo-form-inline">
        <el-form-item label="目录名称" size="mini">
          <el-input v-model="queryInfo.directoryName"></el-input>
        </el-form-item>
        <el-form-item label="状态" size="mini" class="status">
          <el-select v-model="queryInfo.state">
            <el-option label="已启用" value="0"></el-option>
            <el-option label="已禁用" value="1"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button size="mini" @click="clear">清除</el-button>
          <el-button type="primary" size="mini" @click="createContent"
            >搜索</el-button
          >
        </el-form-item>
        <el-form-item class="add">
          <el-link
            type="primary"
            class="el-icon-back"
            :underline="false"
            @click="back"
            v-show="showRentParise"
            >返回学科</el-link
          >
          <el-button
            type="success"
            icon="el-icon-edit"
            size="small"
            @click="addSubject"
            >新增学科</el-button
          >
        </el-form-item>
      </el-form>
      <!-- 数据总数 警告框-->
      <el-alert type="info" title="1111 " :closable="false" show-icon>
        <template slot="title"> 数据一共有 {{ this.total }} 条 </template>
      </el-alert>
      <!-- 表格的总数据 -->
      <template>
        <el-table :data="contentList" stripe style="width: 100%">
          <el-table-column label="序号" type="index" width="70px">
          </el-table-column>
          <el-table-column prop="subjectName" label="所属学科">
          </el-table-column>
          <el-table-column prop="directoryName" label="目录名称">
          </el-table-column>
          <el-table-column prop="username" label="创建者"> </el-table-column>
          <el-table-column prop="addDate" label="创建日期">
            <template v-slot="scope">
              {{ scope.row.addDate | dateFormat }}
            </template>
          </el-table-column>
          <el-table-column prop="totals" label="面试题数量"> </el-table-column>
          <el-table-column prop="state" label="状态" v-model="queryInfo.state">
            <template v-slot="scope">
              {{ scope.row.state === 1 ? '已禁用' : '已启用' }}
            </template>
          </el-table-column>
          <el-table-column label="操作">
            <template v-slot="{ row }">
              <el-link
                :underline="false"
                type="primary"
                @click="changeContent(row)"
                >{{ row.state === 1 ? '启用' : '禁用' }}</el-link
              >
              <el-link
                :underline="false"
                type="primary"
                :disabled="row.state ? true : false"
                @click="amendContent(row)"
                >修改
              </el-link>
              <el-link
                :underline="false"
                type="primary"
                :disabled="row.state ? true : false"
                @click="deleteCotent(row.id)"
                >删除</el-link
              >
            </template>
          </el-table-column>
        </el-table>
      </template>
      <!-- 分页器 -->
      <div class="block">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :page-sizes="[2, 5, 10]"
          :page-size="queryInfo.pagesize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
      </div>
      <!-- 新增目录的弹框 -->
      <el-dialog title="新增目录" :visible.sync="dialogFormVisible">
        <el-form
          :model="content"
          :rules="rules"
          ref="activeFormRef"
          label-width="100px"
        >
          <el-form-item label="所属学科" v-show="!showRentParise">
            <el-select
              placeholder="请选择"
              size="mini"
              v-model="content.subjectID"
            >
              <el-option
                v-for="item in activeForm"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="目录名称" prop="directoryName">
            <el-input size="mini" v-model="content.directoryName"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="addContent">确 定</el-button>
        </div>
      </el-dialog>
      <!-- 修改目录的弹框 -->
      <el-dialog title="修改目录" :visible.sync="ammendFormVisible">
        <el-form
          ref="activeFormRef"
          label-width="100px"
          :model="amend"
          :rules="rules"
        >
          <el-form-item label="所属学科">
            <el-select size="mini" v-model="amend.subjectID">
              <el-option
                v-for="item in activeForm"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="目录名称" prop="directoryName">
            <el-input size="mini" v-model="amend.directoryName"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="ammendFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="amendContentTrue">确 定</el-button>
        </div>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
import {
  getContent,
  changeContentState,
  deleteContentItem,
  addContentItem,
  dropDirectory,
  amendContentItem
} from '@/api/subject/directorys'

// 导入自己的css
import '../../style/directorys.css'
export default {
  name: 'directorys',
  components: {},
  props: {},
  data() {
    return {
      // 数据显示隐藏
      showRentParise: false,
      formInline: {
        true: '',
        false: ''
      },
      value: '',
      queryInfo: {
        page: 1,
        pagesize: 10,
        subjectID: this.$route.query.id,
        directoryName: '',
        state: null
      },
      rules: {
        directoryName: [
          { required: true, message: '请输入活动名称', trigger: 'blur' },
          { min: 3, message: '长度在 3 个字符', trigger: 'blur' }
        ]
      },
      // 总数
      total: 0,
      // 存放所有的数据
      contentList: [],
      // 增加的弹框的出现
      dialogFormVisible: false,
      // 修改的弹框的出现
      ammendFormVisible: false,
      activeForm: [],
      content: {
        subjectID:
          this.$route.query.id && typeof this.$route.query.id === 'string'
            ? parseInt(this.$route.query.id)
            : this.$route.query.id,
        directoryName: ''
      },
      amend: {
        subjectName: '',
        directoryName: '',
        subjectID: null
      },
      newContent: {
        id: '',
        state: null
      }
    }
  },
  computed: {},
  watch: {
    
  },
  async created() {
    //  存储数据
    // console.log(this.$route.query)
    if (this.$route.query.id) {
      const { data } = await getContent(this.queryInfo)
      this.contentList = data.items
      this.total = data.counts
      this.showRentParise = true
    } else {
      this.createContent()
    }
  },
  mounted() {},
  methods: {
    // 获取所有的数据
    async createContent() {
      try {
        const { data } = await getContent(this.queryInfo)
        this.contentList = data.items
        this.total = data.counts
        // if (this.$route.query.id) {
        //   console.log(this.$route.query.id)
        //   const { data: res } = await getContentById(this.$route.query.id)
        //   this.newContent.state = res.state
        //   // console.log(this.newContent.state)
        //   // console.log(res.state)
        //   console.log(res)
        // } else {
        // }
      } catch (err) {
        this.$message('获取失败')
      }
    },
    // 监听 pagesize 几条
    handleSizeChange(newsize) {
      this.queryInfo.pagesize = newsize
      this.createContent()
    },
    // 回退
    back() {
      this.$router.back(-1)
    },
    // 监听页码
    handleCurrentChange(newpage) {
      this.queryInfo.page = newpage
      this.createContent()
    },
    // 清空按钮
    clear() {
      this.queryInfo.directoryName = ''
      this.queryInfo.state = null
      this.createContent()
    },
    // 禁用状态
    async changeContent(val) {
      this.newContent.id = val.id
      this.newContent.state = val.state
      // console.log(val)
      // console.log(this.contentList[index])
      if (this.newContent.state === 1) {
        this.newContent.state = 0
      } else {
        this.newContent.state = 1
      }
      try {
        await changeContentState(this.newContent)
        this.createContent()
      } catch (err) {
        this.$message('失败')
      }
    },
    // 下拉菜单
    async addSubject() {
      try {
        this.dialogFormVisible = true
        const { data } = await dropDirectory()
        // console.log(data)
        this.activeForm = data
      } catch (err) {
        this.$message('获取失败')
      }
    },
    // 新增
    async addContent() {
      try {
        const { data } = await addContentItem(this.content)
        console.log(data)
        console.log(data.id)
        this.content.subjectID = data.id
        this.content = data
        console.log(this.content)
        // console.log(this.content.subjectID)
        this.createContent()
        this.dialogFormVisible = false
      } catch (err) {
        this.$message('添加失败')
      }
    },
    // 修改
    amendContent(data) {
      // 渲染下拉菜单的数据
      this.addSubject()
      // console.log(data)
      this.dialogFormVisible = false
      this.ammendFormVisible = true
      this.amend.subjectName = data.subjectName
      this.amend.directoryName = data.directoryName
      this.amend.subjectID = data.subjectID

      this.amend.id = data.id
    },
    // 确定修改
    async amendContentTrue() {
      // console.log(this.amend)
      try {
        await amendContentItem(this.amend)
        this.createContent()
        this.ammendFormVisible = false
      } catch (err) {
        this.$message('修改失败')
      }
    },
    // 删除
    async deleteCotent(id) {
      try {
        await deleteContentItem(id)
        this.createContent()
        this.$message.success('删除成功')
      } catch (err) {
        this.$message('失败')
      }
    }
  }
}
</script>

<style scoped lang="less"></style>
